<?php
/*
 * variable/load.phtml
 */

use RHX\Helper\RHDateTime;
use RHX\Helper\Number;
use RHX\Model\Object\XRules;
use RHX\Model\Object\XCalculator;
?>
<?php
// variables
/**
 * @var RHX\Model\Manager\VariableManager $manager Manager des Variables
 */
$manager	 = $this->manager;
$post		 = $this->post;
$employes	 = $this->employe;
$periode	 = $this->periode;
$nbSemaine	 = 0;
$calculator = new XCalculator($manager->getAdapter());
?>
<table class='table table-striped table-condensed list-table' style='font-size: 0.9em;'>
    <thead>
	<tr>
	    <td colspan="5">Variable de paie</td>
	    <td colspan='3'>Entrée/Sortie sur le Mois</td>
	    <td colspan='7'>Durée Hebdomadaire</td>
	    <td colspan='6'>Informations (en jours)</td>
	    <td colspan='6'>Informations (en heures)</td>
	    <td colspan='3'>Compteurs</td>
	    <td colspan='7'>Contrat Hebdomadaire</td>
	    <td colspan="5">Taux Horaire Brut</td>
	    <td rowspan="2">Coef</td>
	    <td colspan="5">Taux Horaire Chargé</td>
	</tr>
	<tr>
	    <td>Mat.</td>
	    <td>Nom</td>
	    <td>Prénom</td>
	    <td>Poste</td>
	    <td>Contrat Horaire</td>
	    <!-- E/S Mois -->
	    <td>Entree</td>
	    <td>Sortie</td>
	    <td>Motif</td>
	    <!-- Durée hebdomadaire -->
	    <td>
		<a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="1">S1</a>
	    </td>
	    <td>
		<a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="2">S2</a>
	    </td>
	    <td>
		<a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="3">S3</a>
	    </td>
	    <td>
		<a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="4">S4</a>
	    </td>
	    <td>
		<a href="#" class="show-planning" data-site="<?php echo $post->site; ?>" data-periode="<?php echo $post->periode; ?>" data-semaine="5">S5</a>
	    </td>
	    <td>
		Total
	    </td>
	    <td>Moyenne</td>
	    <!-- Informations Jour -->
	    <td>CP</td>
	    <td>CSS</td>
	    <td>AM1</td>
	    <td>AM2</td>
	    <td>AT</td>
	    <td>ABS</td>
	    <!-- Informations Heure -->
	    <td>CP</td>
	    <td>CSS</td>
	    <td>AM1</td>
	    <td>AM2</td>
	    <td>AT</td>
	    <td>ABS</td>
	    <!-- Compteur -->
	    <td>Retard</td>
	    <td>Heures Planifiées</td>
	    <td>Résultat</td>
	    <!-- Contrat Hebdomadaire -->
	    <td>S1</td>
	    <td>S2</td>
	    <td>S3</td>
	    <td>S4</td>
	    <td>S5</td>
	    <td>Total</td>
	    <td>Moyenne</td>
	    <!-- Taux horaire brute -->
	    <td>S1</td>
	    <td>S2</td>
	    <td>S3</td>
	    <td>S4</td>
	    <td>S5</td>
	    <!-- Taux horaire chargé -->
	    <td>S1</td>
	    <td>S2</td>
	    <td>S3</td>
	    <td>S4</td>
	    <td>S5</td>
	    <!-- Coef -->
	</tr>
    </thead>
    <tbody>
	<?php $jaune		 = 1; ?>
	<?php foreach ($employes as $employe): ?>
	    <?php
	    $colorJaune	 = (($jaune % 2) == 0) ? '#FFEE05' : '#FFF67F';
	    $colorVert	 = ($jaune % 2) == 0 ? '#174f00' : '#278700';
	    $poste		 = $employe->getPoste();
	    $dataCP		 = $employe->getDataCP();
	    $semaines	 = $manager->getEmployeStat($employe->matricule, $periode->id);
	    //var_dump($semaines);
	    $semaineCount	 = 0;
	    $somme		 = 0.0;
	    ?>
    	<tr>
    	    <!-- Informations générale -->
    	    <td style='background: <?php echo $colorJaune; ?>'>
    		<a href="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'edit', 'id' => $employe->matricule)) ?>" target="_blank"><?php echo $employe->matricule; ?></a>
    	    </td>
    	    <td style='background: <?php echo $colorJaune; ?>'>
    		<a href="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'edit', 'id' => $employe->matricule)) ?>" target="_blank"><?php echo $employe->nom1; ?></a>
    	    </td>
    	    <td style='background: <?php echo $colorJaune; ?>'>
    		<a href="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'edit', 'id' => $employe->matricule)) ?>" target="_blank"><?php echo $employe->prenom; ?></a>
    	    </td>
    	    <td style='background: <?php echo $colorJaune; ?>'>
		    <?php echo $poste->code; ?>
    	    </td>
    	    <td style='background: <?php echo $colorJaune; ?>'>
		    <?php echo $employe->getHoraireHebdo(true); ?>
    	    </td>
    	    <!-- Entrée/Sortie du Mois -->
		<?php
		$entree		 = ($manager->isEntreeMonth($employe, $periode)) ? $employe->contrat_debut : '';
		$sortie		 = ($manager->isSortieMonth($employe, $periode)) ? $employe->contrat_fin : '';
		?>
    	    <td>
		    <?php
		    if ($entree != '') {
			$entree = new RHDateTime($entree);
			echo $entree->date('d/m/Y');
		    }
		    ?>
    	    </td>
    	    <td>
		    <?php
		    if ($sortie != '') {
			$sortie = new RHDateTime($sortie);
			echo $sortie->date('d/m/Y');
		    }
		    ?>
    	    </td>
    	    <td>
		    <?php
		    if ($entree != '') {
			echo 'Embauche';
		    }
		    if ($sortie != '') {
			try {
			    $motif = $employe->getMotif();
			    echo '<br>' . $motif->libelle;
			}
			catch (Exception $ex) {
			    echo '<br>Aucun';
			}
		    }
		    ?>
    	    </td>
    	    <!-- Durée Hebdomadaire -->
		<?php for ($i = 0; $i < 5; $i++): ?>
		    <td>
                        <?php 
                            if (isset($semaines[$i])): 
                                $semaine = $semaines[$i];
                                $calculator		 = new \RHX\Model\Object\XCalculator($employe->getAdapter());

                                $dayLundi = $semaines[$i]['lundi']->date;
                                //var_dump($dayLundi);
                                $plan = $planningSemTbl->getByEmploye($employe->matricule, $dayLundi);

                                $modulee = RHDateTime::toFloat( $calculator->getHeuresModulees($plan)->totalHours() ); 
                                $somme += $modulee;
                                 if ($manager->isCount($semaine)) {
                                    $semaineCount++;
                                    $nbSemaine = $semaineCount;
                                }
                                
                                echo '<b>' . Number::frenchFormat($modulee) . '</b>';
                            else:    
                                echo '0,00' ;
                            endif;
                        ?>
                                <!-- obsolete : bug > calcul diff day sans tenir compte des code 
                                <?php //if (isset($semaines[$i])): ?>
                                    <?php /*
                                    $semaine = $semaines[$i];
                                    $total	 = $manager->getSumInWeek($semaine);
                                    $somme += $total;
                                    if ($manager->isCount($semaine)) {
                                        $semaineCount++;
                                        $nbSemaine = $semaineCount;
                                    }
                                    echo '<b>' . Number::frenchFormat($total) . '</b>';
                                    */
                                    ?></b>
                                <?php //else: ?>
                                    <?php //echo '0,00' ?>
                                <?php //endif; ?>-->
		    </td>
		<?php endfor; ?>
    	    <td style="background: <?php echo $colorVert; ?>; color: #fff;">
		    <?php echo Number::frenchFormat($somme); ?>
    	    </td>
    	    <td style='background: <?php echo $colorJaune; ?>'>
		    <?php
		    if ($semaineCount != 0) {
			$moyenne = $somme / $semaineCount;
			echo '<b>' . Number::frenchFormat($moyenne) . '</b>';
		    }
		    else {
			echo Number::frenchFormat(0.0);
		    }
		    ?>
    	    </td>
    	    <!-- Informations -->
		<?php
		$cpNb		 = $manager->getOccurenceInPeriode(XRules::CongePaye, $semaines);
		$cssNb		 = $manager->getOccurenceInPeriode(XRules::CongeSansSolde, $semaines);
		$am1Nb		 = $manager->getOccurenceInPeriode(XRules::ArretMaladie1, $semaines);
		$am2Nb		 = $manager->getOccurenceInPeriode(XRules::ArretMaladie2, $semaines);
		$atNb		 = $manager->getOccurenceInPeriode(XRules::AccidentTravail, $semaines);
		$absNb		 = $manager->getOccurenceInPeriode(XRules::Absent, $semaines);
		?>
    	    <td>
		    <?php echo ($cpNb > 0) ? "<b>$cpNb</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($cssNb > 0) ? "<b>$cssNb</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($am1Nb > 0) ? "<b>$am1Nb</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($am2Nb > 0) ? "<b>$am2Nb</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($atNb > 0) ? "<b>$atNb</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($absNb > 0) ? "<b>$absNb</b>" : ''; ?>
    	    </td>
	    <!-- Informations Heures -->
		<?php
		$cpH		 = Number::frenchFormat($manager->getHeureCodeInPeriode(XRules::CongePaye, $semaines));
		$cssH		 = Number::frenchFormat($manager->getHeureCodeInPeriode(XRules::CongeSansSolde, $semaines));
		$am1H		 = Number::frenchFormat($manager->getHeureCodeInPeriode(XRules::ArretMaladie1, $semaines));
		$am2H		 = Number::frenchFormat($manager->getHeureCodeInPeriode(XRules::ArretMaladie2, $semaines));
		$atH		 = Number::frenchFormat($manager->getHeureCodeInPeriode(XRules::AccidentTravail, $semaines));
		$absH		 = Number::frenchFormat($manager->getHeureCodeInPeriode(XRules::Absent, $semaines));
		?>
    	    <td>
		    <?php echo ($cpH > 0) ? "<b>$cpH</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($cssH > 0) ? "<b>$cssH</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($am1H > 0) ? "<b>$am1H</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($am2H > 0) ? "<b>$am2H</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($atH > 0) ? "<b>$atH</b>" : ''; ?>
    	    </td>
    	    <td>
		    <?php echo ($absH > 0) ? "<b>$absH</b>" : ''; ?>
    	    </td>
    	    <!-- Compteur -->
		<?php
		//Zend\Debug\Debug::dump($semaines);
		$nbRetard	 = $manager->getOccurenceRetardInPeriode($semaines);
		$tpsRetard	 = $manager->getTimeRetardInPeriode($semaines);
		?>
    	    <td>
		    <?php echo Number::frenchFormat(RHDateTime::toFloat($tpsRetard)) . " ($nbRetard)"; ?>
    	    </td>
    	    <td>
    		<b><?php echo Number::frenchFormat($somme); ?></b>
    	    </td>
		<?php
		$countSemaine	 = $semaineCount;
		$result		 = $somme + RHDateTime::toFloat($tpsRetard) - ($employe->getHoraireHebdo(false) * $countSemaine);
		?>
    	    <td style='background: <?php echo $colorJaune; ?>' title="<?php echo $somme . ' + ' . RHDateTime::toFloat($tpsRetard) . ' - (' . $employe->getHoraireHebdo(false) . ' x ' . $countSemaine . ') = ' . $result; ?>">
    		<b><?php echo Number::frenchFormat($result); ?></b>
    	    </td>
    	    <!-- Hebdomadaire -->
		<?php $totalHebdo	 = 0.0; ?>
		<?php for ($i = 0; $i < 5; $i++): ?>
		    <td>
			<?php if (isset($semaines[$i])): ?>
			    <?php
			    $semaine = $semaines[$i];
			    if ($manager->isCount($semaine)) {
				$hebdo = $manager->getProrataHeureHebdomadaire($employe->getHoraireHebdo(), $employe->matricule, $semaine);
				$totalHebdo += $hebdo;
				//echo '<b>' . $employe->getHoraireHebdo(true) . '</b>';
				echo '<b>' . Number::frenchFormat($hebdo) . '</b>';
			    }
			    else {
				echo '0,00';
			    }
			    ?>
			<?php else: ?>
			    <?php echo '0,00' ?>
			<?php endif; ?>
		    </td>
		<?php endfor; ?>
    	    <td style="background: <?php echo $colorVert; ?>; color: #fff;">
		    <?php echo Number::frenchFormat($totalHebdo); ?>
    	    </td>
    	    <td style='background: <?php echo $colorJaune; ?>'>
    		<b><?php echo ($nbSemaine != 0) ? Number::frenchFormat($totalHebdo / $nbSemaine) : '0,00'; ?></b>
    	    </td>
    	    <!-- Taux horaire brute -->
		<?php for ($i = 0; $i < 5; $i++): ?>
		    <td>
			<?php if (isset($semaines[$i])): ?>
			    <?php
			    $semaine = $semaines[$i];
			    if ($manager->isCount($semaine)) {
				echo '<b>' . Number::frenchFormat($dataCP['taux']) . '</b>';
			    }
			    else {
				echo '0,00';
			    }
			    ?>
			<?php else: ?>
	    		0,00
			<?php endif; ?>
		    </td>
		<?php endfor; ?>

    	    <td style='background: <?php echo $colorJaune; ?>'>
    		<b><?php echo Number::frenchFormat($employe->getCoefficient()); ?></b>
    	    </td>
    	    <!-- Taux horaire chargé -->
		<?php for ($i = 0; $i < 5; $i++): ?>
		    <td>
			<?php if (isset($semaines[$i])): ?>
			    <?php
			    $semaine = $semaines[$i];
			    if ($manager->isCount($semaine)) {
				echo '<b>' . Number::frenchFormat($dataCP['taux'] * $dataCP['coef']) . '</b>';
			    }
			    else {
				echo '0,00';
			    }
			    ?>
			<?php else: ?>
	    		0,00
			<?php endif; ?>
		    </td>
		<?php endfor; ?>
    	</tr>
	    <?php $jaune++; ?>
	<?php endforeach; ?>
    </tbody>
</table>

<script type="text/javascript">
// Démarrage
    $(function() {
	// Affichage des plannings en consultation
	$('.show-planning').click(function(e) {
	    e.preventDefault();
	    semaine = $(this).attr('data-semaine');
	    periode = $(this).attr('data-periode');
	    site = $(this).attr('data-site');
	    // Récupération de la date de la semaine
	    ajaxS = $.ajax({
		dataType: 'json',
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller' => 'variable', 'action' => 'getSemaine')) ?>',
		data: '&periode=' + periode + '&semaine=' + semaine
	    });
	    ajaxS.success(function(data) {
		// Récupération du template de création du site
		ajaxT = $.ajax({
		    dataType: 'json',
		    type: 'post',
		    url: '<?php echo $this->url('RHX/default', array('controller' => 'variable', 'action' => 'getTemplate')) ?>',
		    data: '&semaine=' + data['semaine'].id + '&site=' + site
		});
		ajaxT.success(function(result) {
		    // Chargement du planning Semaine
		    dataPlanning = '&data-lundi=' + data['semaine'].lundi + '&template=' + result.template + '&site_id=' + site + '&editable=0&lundi=on&mardi=on&mercredi=on&jeudi=on&vendredi=on&debut=0000-00-00&fin=0000-00-00&dialog=1';
		    ajaxP = $.ajax({
			type: 'post',
			url: '<?php echo $this->url('RHX/default', array('controller' => 'planning', 'action' => 'edit')) ?>',
			data: dataPlanning
		    });
		    ajaxP.success(function(planning) {
			width = $(document).width() - 20;
			height = $(document).height() - 20;
			$("#dialog1").hide().html(planning).fadeIn('normal').dialog({
			    width: width,
			    height: height,
			    title: "Planning Semaine du " + data['semaine'].lundi + " au " + data['semaine'].dimanche,
			    autoOpen: true,
			    show: {effect: 'fade', duration: 1000},
			    hide: {effect: 'fade', duration: 700}
			});
		    });
		    ajaxP.error(function() {
		    });
		});
		ajaxT.error(function() {
		});
	    });
	    ajaxS.error(function() {
		//
	    });
	});
    });
</script>

<style type='text/css'>
    .jaune1{
	background: #FFF67F;
    }
    .jaune2{
	background: #FFEE05;
    }
    .show-planning{
	display: block;
	width: 100%;
	height: 100%;
    }
    .RT {
	background: #006ACE;
    }
    .DA{
	background: #a600c3;
    }
    .vert1{
	background: #278700;
	color: #fff;
    }
    .vert2{
	background: #174f00;
	color: #fff;
    }
</style>